package qldc.pm.tiensu.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import qldc.pm.tiensu.constants.TienSuConstants;
import qldc.pm.tiensu.dto.CongViecBanGiaoDTO;
import qldc.pm.tiensu.model.BienBanBanGiaoHoSo;
import qldc.pm.tiensu.service.BienBanBanGiaoHoSoLocalServiceUtil;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.json.JSONArray;
import com.liferay.portal.kernel.json.JSONException;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.servlet.ServletResponseUtil;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.servlet.SessionMessages;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.model.User;
import com.liferay.portal.util.PortalUtil;
import com.report.logic.Report;

public class LapBienBanBanGiaoHoSo {
	private static Log _log = LogFactoryUtil.getLog(LapBienBanBanGiaoHoSo.class);

	private static SimpleDateFormat DEFAULT_DATE_FORMAT = new SimpleDateFormat(TienSuConstants.DATE_PATTERN);

	public void xoaBienBan(ActionRequest request, ActionResponse response) throws SystemException, PortalException, IOException {
		long bienBanBanGiaoHoSoId = ParamUtil.getLong(request, "bienBanBanGiaoHoSoId");

		if (bienBanBanGiaoHoSoId > 0) {
			BienBanBanGiaoHoSo bienBanBanGiaoHoSo = BienBanBanGiaoHoSoLocalServiceUtil.findBienBanBanGiaoHoSoId(bienBanBanGiaoHoSoId);
			BienBanBanGiaoHoSoLocalServiceUtil.deleteBienBanBanGiaoHoSo(bienBanBanGiaoHoSo);
		}

		String redirect = ParamUtil.getString(request, "redirect");
		response.sendRedirect(redirect);
	}

	public void luuHoacInBienBan(ActionRequest request, ActionResponse response) throws SystemException, PortalException, IOException {
		String cmd = ParamUtil.getString(request, "cmd","");

		try {
			User user = PortalUtil.getUser(request);

			if (Validator.isNotNull(user)) {
				if(cmd.equals(TienSuConstants.IN)) {
					inBienBan(request,response);
				} else {
					luuBienBan(request,response);

					SessionMessages.add(request, "luu-bien-ban-ban-giao-ho-so-thanh-cong");
				}
			}
		} catch (Exception e) {
			_log.warn(e);
			SessionErrors.add(request, "thao-tac-khong-thanh-cong");
		}

		String redirect = ParamUtil.getString(request, "redirect");
		response.sendRedirect(redirect);
	}

	private void luuBienBan(ActionRequest request, ActionResponse response) throws SystemException, JSONException {
		String kinhGui = ParamUtil.getString(request, "kinhGui","");
		String diaDiemBanGiao = ParamUtil.getString(request, "diaDiemBanGiao");
		String nguoiLap = ParamUtil.getString(request, "nguoiLap");
		String chucVu = ParamUtil.getString(request, "chucVu");
		String nguoiDuocBanGiao = ParamUtil.getString(request, "nguoiDuocBanGiao");
		String chucVuNguoiDuocBanGiao = ParamUtil.getString(request, "chucVuNguoiDuocBanGiao");
		String noiDung = ParamUtil.getString(request, "noiDung");

		Date ngayLap = ParamUtil.getDate(request, "ngayLap", DEFAULT_DATE_FORMAT, null);
		Date ngayBanGiao = ParamUtil.getDate(request, "ngayBanGiao", DEFAULT_DATE_FORMAT, null);

		long bienBanBanGiaoHoSoId = ParamUtil.getLong(request, "bienBanBanGiaoHoSoId");

		String congViecStr =  ParamUtil.getString(request, "congViecs");

		List<CongViecBanGiaoDTO> congViecs = convertJsonToCongViecBanGiaoDTOs(congViecStr);

		if (bienBanBanGiaoHoSoId > 0) {
			BienBanBanGiaoHoSoLocalServiceUtil.updateBienBan(bienBanBanGiaoHoSoId, kinhGui, nguoiLap, nguoiDuocBanGiao, chucVu, chucVuNguoiDuocBanGiao, noiDung, ngayLap, ngayBanGiao,diaDiemBanGiao, congViecs);
		} else {
			BienBanBanGiaoHoSoLocalServiceUtil.addBienBan(kinhGui, nguoiLap, nguoiDuocBanGiao, chucVu, chucVuNguoiDuocBanGiao, noiDung, ngayLap, ngayBanGiao, diaDiemBanGiao, congViecs);
		}
	}

	private List<CongViecBanGiaoDTO> convertJsonToCongViecBanGiaoDTOs(String congViecs) throws JSONException {
		JSONArray jsons = JSONFactoryUtil.createJSONArray(congViecs);

		JSONObject jsonQuaTrinh;

		List<CongViecBanGiaoDTO> result = new ArrayList<CongViecBanGiaoDTO>();

		CongViecBanGiaoDTO cvto;

		for (int i = 0; i < jsons.length(); i++) {
			jsonQuaTrinh = jsons.getJSONObject(i);
			cvto = new CongViecBanGiaoDTO();
			cvto.setChuThich(jsonQuaTrinh.getString("chuThich"));
			cvto.setTenCongViec(jsonQuaTrinh.getString("tenCongViec"));
			cvto.setTienDoThucHien(jsonQuaTrinh.getString("tienDoThucHien"));
			if (Validator.isNotNull(cvto.getChuThich()) || Validator.isNotNull(cvto.getTenCongViec())  || Validator.isNotNull(cvto.getTienDoThucHien())) {
				result.add(cvto);
			}
		}

		return result;
	}

	private void inBienBan(ActionRequest request,ActionResponse response) throws IOException, JSONException {
		String nguoiLap = ParamUtil.getString(request, "nguoiLap","");
		String nguoiDuocBanGiao = ParamUtil.getString(request, "nguoiDuocBanGiao","");
		String chucVuNguoiDuocBanGiao = ParamUtil.getString(request, "chucVuNguoiDuocBanGiao","");
		String chucVu = ParamUtil.getString(request, "chucVu","");
		String noiDung = ParamUtil.getString(request, "noiDung","");
		Date ngayLap = ParamUtil.getDate(request, "ngayLap",DEFAULT_DATE_FORMAT);
		Date ngayBanGiao = ParamUtil.getDate(request, "ngayBanGiao", DEFAULT_DATE_FORMAT, null);
		String congViecStr =  ParamUtil.getString(request, "congViecs","");
		List<CongViecBanGiaoDTO> congViecs = convertJsonToCongViecBanGiaoDTOs(congViecStr);
		String diaDiemBanGiao = ParamUtil.getString(request, "diaDiemBanGiao","");

		HashMap<String, Object> data = new HashMap<String, Object>();
		data.put("chucVu", chucVu);
		data.put("diaDiemBanGiao", diaDiemBanGiao);
		data.put("noiDung", noiDung);
		data.put("nguoiLap", nguoiLap);
		data.put("ngayLap", ngayLap);
		data.put("ngayBanGiao", ngayBanGiao);
		data.put("nguoiDuocBanGiao", nguoiDuocBanGiao);
		data.put("chucVuNguoiDuocBanGiao", chucVuNguoiDuocBanGiao);
		data.put("list_congviec", congViecs);
		PortletContext pc = request.getPortletSession().getPortletContext();
		String reportFileName = TienSuConstants.BIEN_BAN_GIAO_HO_SO;
		String reportFilePath = pc
				.getRealPath(TienSuConstants.REPORT_TEMPLATE_FOLDER + "/"
						+ reportFileName + ".jrxml");

		File file = Report.createReport(data, Report.DOC_TYPE, reportFilePath,
				reportFileName);
		if (Validator.isNotNull(file)) {
			InputStream in = new FileInputStream(file);

			HttpServletRequest httpServletRequest = PortalUtil
					.getHttpServletRequest(request);
			HttpServletResponse httpServletResponse = PortalUtil
					.getHttpServletResponse(response);
			ServletResponseUtil.sendFile(httpServletRequest,
					httpServletResponse, reportFileName + "."
							+ Report.DOC_TYPE, in, Report.DOC_STREAM_TYPE);
		}
	}
}
